//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class FormatRecord : WorkbookLevelRecord
    {
        public FormatRecord()
        {

        }

        public FormatRecord(string code, ushort id)
        {
            this.code = (XLUnicodeString)code;
            this.id = id;
        }
        private XLUnicodeString code;
        private ushort id;

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.Format; }
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            context.Writer.Write(id);
            code.Write(context);
        }

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            id = context.ReadUInt16();
            code.Read(context.Reader);
        }

        public override void Apply(BiffReaderWorkbookContext context)
        {
            if (!context.FormatTable.ContainsKey(id))
                context.FormatTable.Add(id, (string)code);
        }
    }
}